package com.whsxt.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whsxt.constant.QueueConstant;
import com.whsxt.domain.User;
import com.whsxt.mapper.UserMapper;
import com.whsxt.model.AliSmsModel;
import com.whsxt.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-21 16:50
 * @Description: ${Description}
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean updateById(User user) {
        log.info("更新用户信息：{}", JSON.toJSONString(user));
        user.setModifyTime(new Date());
        user.setUserLasttime(new Date());
        int update = userMapper.updateById(user);
        return update > 0;
    }


    /**
     * 发送注册验证码
     * 1、生成一个code
     * 2、放到redis
     * 3、设置过期时间
     * 4、组装一个参数
     * 5、放入mq
     * 6、返回
     *
     * @param openId
     * @param sendMap
     */
    @Override
    public void sendSms(String openId, Map<String, String> sendMap) {
        String phonenum = sendMap.get("phonenum");
        //生成验证码
        String code = createCode();
        redisTemplate.opsForValue().set(phonenum, code, Duration.ofMinutes(5));
        //组装参数，放mq
        AliSmsModel aliSmsModel = new AliSmsModel();
        aliSmsModel.setPhoneNumbers(phonenum);
        aliSmsModel.setSignName("ego商城");
        aliSmsModel.setTemplateCode("SMS_203185255");
        HashMap<String, String> map = new HashMap<>();
        map.put("code", code);
        aliSmsModel.setTemplateParam(JSON.toJSONString(map));
        //放mq
        rabbitTemplate.convertAndSend(QueueConstant.PHONE_SEND_EX, QueueConstant.PHONE_SEND_KEY, JSON.toJSONString(aliSmsModel));
    }

    private String createCode() {
        return "888888";
    }


    /**
     * 保存手机号
     * 1、判断redis里有没有
     * 2、
     *
     * @param openId
     * @param sendMap
     */
    @Override
    public void savePhone(String openId, Map<String, String> sendMap) {
        String phonenum = sendMap.get("phonenum");
        String redisCode = redisTemplate.opsForValue().get(phonenum);
        //判断code是否一致
        String code = sendMap.get("code");
        if (!StringUtils.isEmpty(redisCode) && code.equals(redisCode)) {
            User user = new User();
            user.setUserMobile(phonenum);
            userMapper.update(user, new LambdaQueryWrapper<User>()
                    .eq(User::getUserId, openId)
            );
            redisTemplate.delete(phonenum);
        }


    }
}
